package domain;

import java.util.Observable;

public class Book extends Observable implements Comparable<Book> {

    @Override
    public int hashCode() {
        return System.identityHashCode(this);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        return false;
    }

    private String title = "", author = "", publisher = "";
    private Shelf shelf;
    private Library library = Library.getInstance();

    public Book(String name) {
        this.title = name;
    }

    public String getName() {
        return title;
    }

    public void setName(String name) {
        this.title = name;
        doNotify();
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String autor) {
        this.author = autor;
        doNotify();
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
        doNotify();
    }

    public Shelf getShelf() {
        return shelf;
    }

    public void setShelf(Shelf shelf) {
        this.shelf = shelf;
        doNotify();
    }

    public boolean isAvailable() {
        if (library.getLentCopiesOfBook(this).size() < library.getCopiesOfBook(this).size()) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return title + ", " + author + ", " + publisher;
    }

    @Override
    public int compareTo(Book that) {
        if (that == null)
            return -1;

        int compareValue = this.title.compareToIgnoreCase(that.title);

        if (compareValue != 0)
            return compareValue;

        compareValue = this.author != null ? this.author.compareToIgnoreCase(that.author) : -1;

        if (compareValue != 0)
            return compareValue;

        compareValue = this.publisher != null ? this.publisher.compareToIgnoreCase(that.publisher) : -1;
        if (compareValue != 0)
            return compareValue;

        compareValue = this.shelf != null ? this.shelf.compareTo(that.shelf) : -1;
        
        compareValue = library.getCopiesOfBook(this).size()-library.getCopiesOfBook(that).size(); 

        return compareValue;
    }

    void doNotify() {
        setChanged();
        notifyObservers();
        Library.getInstance().doNotify();
    }
}
